

import axios from 'axios'
import { Message } from 'element-ui'
import QS from "qs";
import store from "@/store";



var base = process.env.NODE_ENV === 'production' ? '': '';

// 创建axios实例
const axiosInstance = axios.create({
  baseURL: base,
  headers:{
    "content-Type":"application/x-www-form-urlencoded"
  }
})

axiosInstance.interceptors.request.use( config => {
  //console.log(config)
  return config
}, error => {
  Promise.reject(error)
})

// respone拦截器
axiosInstance.interceptors.response.use(
  response => {
    if(response.data){
      if(response.data.code == 200 || response.data.code == 0){
          return Promise.resolve(response.data.data);
      }else if(response.data.code == 1003){
          //store.commit("setUserInfo","");
          
        return Promise.reject(response.data)
      }else{
        var msg = response.data.msg || "服务器错误";
        Message.error(msg);
        return Promise.reject({code:500,msg:msg,step:1})
      }
    }else{
      Message.error("服务器错误");
      return Promise.reject({code:500,msg:"服务器错误",step:2})
    }
  },
  error => {

    Message.error("网络错误");
    return Promise.reject(error)
  })


export default {
    get: function(url,params){
      return axiosInstance.get(url,{params});
    },
    post: function(url,params){
      return axiosInstance.post(url,QS.stringify(params));
    },
    fromData:axiosInstance.post
  }

